package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.SalarySocialInsurance;
import com.sihai.ehr.service.SalaryInsuranceService;

/**
 * 社保管理服务实现
 * 
 * @author henry
 * 
 */
@Service("salaryInsuranceService")
public class SalaryInsuranceServiceImpl implements SalaryInsuranceService {

	@Resource
	private DataAccessor dataAccessor;

	@Override
	public SalarySocialInsurance get(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(SalarySocialInsurance.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取定性考核员工分数明细数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：SalaryInsuranceServiceImpl.get;参数id=" + id + " 】");
		}
	}

	@Override
	public void save(SalarySocialInsurance insurance) {
		if (insurance == null || insurance.getOrganization() == null) {
			throw new RuntimeException("【insurance对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(insurance);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保存社保信息对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：SalaryInsuranceServiceImpl.insurance】");
		}
	}

	@Override
	public void deleteSocialInsurances(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			//第一步移出社保细节
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from SalarySocialInsuranceDetail s where s.socialInsurance.id in ({0})",
									ids));
				}
			});
			//移出社保
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from SalarySocialInsurance s where s.id in ({0})",
									ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出社保信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：SalaryInsuranceServiceImpl.deleteSocialInsurances】");
		}
	}
	
	@Override
	public void deleteSocialInsuranceDetails(final Long insuranceId) {
		if(insuranceId == null && insuranceId <= 0){
			throw new RuntimeException("【参数异常：insuranceId参数错误】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from SalarySocialInsuranceDetail d where d.socialInsurance.id={0}",
									insuranceId));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出社保项目细节信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：SalaryInsuranceServiceImpl.deleteSocialInsuranceDetails】");
		}
	}

	@Override
	public LimitedList<SalarySocialInsurance> find(final Long organizationId,
			Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(SalarySocialInsurance.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from SalarySocialInsurance s where 1 <> 2");
							dynamicQuery.appendIfNotNull(
									"and s.organization.id =:organizationId",
									"organizationId", organizationId);
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核周期列表时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：SalaryInsuranceServiceImpl.find;参数organizationId="
							+ organizationId + "】");
		}
	}
	
	@Override
	public LimitedList<SalarySocialInsurance> searchInsuranceList(final Long organizationId) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(SalarySocialInsurance.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"select new com.sihai.ehr.model.biz.SalarySocialInsurance(id,title) from SalarySocialInsurance s where 1 <> 2");
							dynamicQuery.appendIfNotNull(
									"and s.organization.id =:organizationId",
									"organizationId", organizationId);
							return dynamicQuery.build(session);
						}
					}, null, null);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核周期列表时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：SalaryInsuranceServiceImpl.searchInsuranceList;参数organizationId="
							+ organizationId + "】");
		}
	}

}
